package log350.tp3;

import classes.UserHandler;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Paint;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

public class VisionnerBilletActivity extends Activity {
	static final int ACHETER_BILLET_CODE = 1;  // The request code
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_visionner_billet);
        creerPath();

        if (UserHandler.getInstance().getUserLoggedIn().getBillet() == null){
        	(findViewById (R.id.lblNomPlayer)).setVisibility(View.INVISIBLE);
        	(findViewById (R.id.lblTypeBillet)).setVisibility(View.INVISIBLE);
        	(findViewById (R.id.lblSeat)).setVisibility(View.INVISIBLE);
        	(findViewById (R.id.lblNomJoueur2)).setVisibility(View.INVISIBLE);
        	(findViewById (R.id.lblTypeBillet2)).setVisibility(View.INVISIBLE);
        	(findViewById (R.id.lblSiege)).setVisibility(View.INVISIBLE);
	       	(findViewById (R.id.imageViewAsus)).setVisibility(View.INVISIBLE);
	       	(findViewById (R.id.imageViewCisco)).setVisibility(View.INVISIBLE);
	    	(findViewById (R.id.imageViewETS)).setVisibility(View.INVISIBLE);
        }
        else{
        	(findViewById (R.id.btnAcheterBillet)).setVisibility(View.INVISIBLE);
	    	(findViewById (R.id.lblPasBillet)).setVisibility(View.INVISIBLE);
        	TextView lblNomJoueur2 = (TextView)(findViewById (R.id.lblNomJoueur2));
        	lblNomJoueur2.setText(UserHandler.getInstance().getUserLoggedIn().getUsername());
        	TextView lblTypeBillet2 =  (TextView)(findViewById (R.id.lblTypeBillet2));
        	lblTypeBillet2.setText(UserHandler.getInstance().getUserLoggedIn().getBillet().getType());
        	TextView lblSiege =  (TextView)(findViewById (R.id.lblSiege));
        	lblSiege.setText(UserHandler.getInstance().getUserLoggedIn().getBillet().getSiege());
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_visionner_billet, menu);
        return true;
    }
    
    public void acheterBillet(View view){
    	Intent intent = new Intent(this, AcheterBilletActivity.class);
    	startActivityForResult(intent, ACHETER_BILLET_CODE);
    }
    
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    	if (resultCode == Activity.RESULT_OK  && requestCode==ACHETER_BILLET_CODE) {
    		 if (UserHandler.getInstance().getUserLoggedIn().getBillet() != null){
    	       	(findViewById (R.id.lblNomPlayer)).setVisibility(View.VISIBLE);
    	       	(findViewById (R.id.lblTypeBillet)).setVisibility(View.VISIBLE);
    	       	(findViewById (R.id.lblSeat)).setVisibility(View.VISIBLE);
    	       	(findViewById (R.id.lblNomJoueur2)).setVisibility(View.VISIBLE);
    	       	(findViewById (R.id.lblTypeBillet2)).setVisibility(View.VISIBLE);
    	       	(findViewById (R.id.lblSiege)).setVisibility(View.VISIBLE);
    	       	(findViewById (R.id.imageViewAsus)).setVisibility(View.VISIBLE);
    	       	(findViewById (R.id.imageViewCisco)).setVisibility(View.VISIBLE);
    	    	(findViewById (R.id.imageViewETS)).setVisibility(View.VISIBLE);
            	(findViewById (R.id.btnAcheterBillet)).setVisibility(View.INVISIBLE);
            	(findViewById (R.id.lblPasBillet)).setVisibility(View.INVISIBLE);
            	
            	TextView lblNomJoueur2 = (TextView)(findViewById (R.id.lblNomJoueur2));
            	lblNomJoueur2.setText(UserHandler.getInstance().getUserLoggedIn().getUsername());
            	TextView lblTypeBillet2 =  (TextView)(findViewById (R.id.lblTypeBillet2));
            	lblTypeBillet2.setText(UserHandler.getInstance().getUserLoggedIn().getBillet().getType());
            	TextView lblSiege =  (TextView)(findViewById (R.id.lblSiege));
            	lblSiege.setText(UserHandler.getInstance().getUserLoggedIn().getBillet().getSiege());
    	    }
    	}else if(resultCode == Activity.RESULT_FIRST_USER){
	    	setResult(RESULT_OK, new Intent());
	        finish();
	    }
	}
    
    public void creerPath(){
    	TableLayout tableLayoutLink = (TableLayout)findViewById(R.id.tblLink);
        tableLayoutLink.removeAllViews();
        
        TableRow tableRowLink = new TableRow(getApplicationContext());            
        
        final TextView lanETS = new TextView(getApplicationContext());
        lanETS.setText("Lan ETS");  
        lanETS.setTextColor(0xFFff8000);
        lanETS.setTextSize(12);
        lanETS.setPaintFlags( lanETS.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
       
        lanETS.setPadding(15, 10, 0, 20);
        lanETS.setClickable(true);
        lanETS.setOnClickListener(new OnClickListener(){
			public void onClick(View arg0) {
				setResult(RESULT_OK, new Intent());
		        finish();
			}

          });
        tableRowLink.addView(lanETS);
        
        final TextView firstComma = new TextView(getApplicationContext());
        firstComma.setText(">");
        firstComma.setTextSize(12);
        firstComma.setPadding(10, 10, 0, 20);
        tableRowLink.addView(firstComma);
        
        final TextView listeParticipant = new TextView(getApplicationContext());
        listeParticipant.setText("Billet");   
        listeParticipant.setTextSize(12);        
        listeParticipant.setPadding(10, 10, 0, 20);
        tableRowLink.addView(listeParticipant);
        
        tableLayoutLink.addView(tableRowLink);
    }
}
